//
//  QTTArrowSearchView.m
//  QuTouTiao
//
//  Created by 杨杨子 on 2018/6/30.
//  Copyright © 2018年 杨杨子. All rights reserved.
//

#import "QTTArrowSearchView.h"
@interface QTTArrowSearchView()

@property(nonatomic,strong)UIButton *searchButton;
@end
@implementation QTTArrowSearchView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.searchTextField = [[UITextField alloc]initWithFrame:CGRectZero];
        [self addSubview:self.searchTextField];
        self.searchTextField.returnKeyType = UIReturnKeySearch;
        self.backgroundColor = [UIColor colorWithRed:237.0/255 green:237.0/255 blue:237.0/255 alpha:1.0];
        self.searchTextField.backgroundColor = [UIColor whiteColor];
        
        UIButton *searchButton = [[UIButton alloc]initWithFrame:CGRectZero];
        self.searchButton = searchButton;
        searchButton.imageEdgeInsets = UIEdgeInsetsMake(2, 5, 2, 0);
        searchButton.contentMode = UIViewContentModeCenter;
        
        [searchButton setImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal];
        [searchButton addTarget:self action:@selector(searchClick:) forControlEvents:UIControlEventTouchUpInside];
        self.searchTextField.leftViewMode = UITextFieldViewModeAlways;
        self.searchTextField.leftView = searchButton;
        
        self.searchTextField.font = [UIFont systemFontOfSize:14];
        self.searchTextField.layer.cornerRadius = 5;
        self.searchTextField.layer.masksToBounds = YES;
        self.searchTextField.delegate = self;
        
    }
    return self;
}
-(void)searchClick:(UIButton *)sender{
    [self.searchTextField resignFirstResponder];
    NSLog(@"aaself.searchTextField.text:%@",self.searchTextField.text);
    self.block(self.searchTextField.text);
}
-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat height = (SCREEN_HEIGHT_NEW < 568 ? 30:50);
    self.searchTextField.frame = CGRectMake(8, 8/height * self.height, self.width - 8*2/height * self.height, self.height - 8 * 2/height * self.height);
    self.searchButton.frame = CGRectMake(10, 0, 20, 20);
}
#pragma -mark- UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"%@",textField.text);
    self.block(self.searchTextField.text);
    [textField resignFirstResponder];
    return YES;
}

@end
